<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">

      <el-form-item label="房源ID:" prop="id">
        <el-input type="number" v-model="queryData.id" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('id')"/>
      </el-form-item>
      <el-form-item label="房源名称:" prop="name">
        <el-input v-model="queryData.name" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('name')"/>
      </el-form-item>
      <el-form-item label="房源类型:" prop="type">
        <el-select v-model="queryData.type" placeholder="请选择认证状态" clearable @clear="formClear('type')"
                   style="width: 200px">
          <el-option label="园区" :value="1"/>
          <el-option label="写字楼" :value="2"/>
          <el-option label="个人" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="地址:" prop="provinces">
        <el-cascader v-model="queryData.provinces" ref="cascader" :props="props" @change="handleChange" clearable
                     @clear="formClear('provinces')" placeholder="请选择" style="width: 200px"/>
      </el-form-item>

      <el-form-item label="房源经纪人名称:" prop="hosingAgentName">
        <el-input v-model="queryData.hosingAgentName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('hosingAgentName')"/>
      </el-form-item>
      <el-form-item label="上架状态:" prop="shelfStatus">
        <el-select v-model="queryData.shelfStatus" placeholder="请选择" clearable @clear="formClear('shelfStatus')"
                   style="width: 200px">
          <el-option label="已上架" :value="1"/>
          <el-option label="未上架" :value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item label="房源状态:" prop="rentStatus">
        <el-select v-model="queryData.rentStatus" placeholder="请选择" clearable @clear="formClear('rentStatus')"
                   style="width: 200px">
          <el-option label="未出租" value="0"/>
          <el-option label="已出租" value="1"/>
          <el-option label="正在出租" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="园区名称:" prop="gardenName">
        <el-input v-model="queryData.gardenName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('gardenName')"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="$router.push('/housingManage/housingAdd')">新 增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="id" label="房源ID" align="center"/>
      <el-table-column prop="name" label="房源名称" align="center"/>
      <el-table-column prop="address" label="地址" align="center" show-overflow-tooltip/>
      <el-table-column prop="infoAddress" label="详情地址" align="center" show-overflow-tooltip/>
      <el-table-column prop="univalenceDay" label="单价" align="center"/>
      <el-table-column prop="housingArea" label="面积" align="center"/>
      <el-table-column prop="type" label="房源类型" align="center"/>
      <el-table-column prop="agentName" label="房源经纪人名称" align="center" width="150"/>
      <el-table-column prop="agentPhone" label="房源经纪人联系方式" align="center" width="150"/>
      <el-table-column prop="belongingName" label="所有者名称" align="center" width="150"/>
      <el-table-column prop="belongingPhone" label="所有者联系方式" align="center" width="150"/>
      <el-table-column prop="rentStatusString" label="房源状态" align="center"/>
      <el-table-column prop="checkStatusString" label="审核状态" align="center"/>
      <el-table-column label="上架状态" align="center" width="100">
        <template slot-scope="{row}">
          <el-switch :value="row.shelfStatus" active-color="#13ce66"
                     :active-value="1" :inactive-value="0" @change="switchChange($event, row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="{row}">
          <el-button type="text" @click="entityInfoEvent(row)">查看</el-button>
          <el-dropdown @command="auditEvent($event, row)" style="margin-left: 10px" v-if="row.checkStatus != 1">
            <div class="row-div el-dropdown-link">
              <div>审核</div>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="1">通过</el-dropdown-item>
              <el-dropdown-item command="2">拒绝</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getProvinces } from '@/utils/common.js'

  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    name: 'housingList',
    data() {
      return {
        tableList: [],
        queryData: {
          provinces: null,
          province: null, // 省
          city: null, // 市
          county: null, // 区
          gardenName: null, // 园区名字
          hosingAgentName: null, // 经纪人名字
          id: null, //
          name: null, // 房源名称
          rentStatus: null, // 租用状态：0未出租；1已出租（不能上架可以删除）；2正在出租
          shelfStatus: null, // 上架状态：0未上架；1上架
          type: null, // 房源类型：1园区；2写字楼 3 个人

          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        props: getProvinces()
      }
    },
    beforeMount() {
      this.getRequest()
    },
    methods: {
      /**
       * 审核事件
       * */
      auditEvent(event, row) {
        if (event == 2) {
          return this.$prompt('请输入拒绝原因', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
          }).then(async({ value }) => {
            if (!value) return this.$message.warning('拒绝原因不能为空')
            await this.$ajax.post(`/housing/source/backstage/check`, { id: row.id, checkStatus: event, checkReason:  value})
            this.$message.success('操作成功!')
            this.getRequest()
          })
        }
        this.$confirm('确认审核通过吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.post(`/housing/source/backstage/check`, { id: row.id, checkStatus: event })
          this.$message.success('操作成功!')
          this.getRequest()
        })
      },
      /**
       * 上下架事件
       */
      switchChange(event, row) {
        this.$confirm('确认要修改当前上下架状态吗?', '提示', this.confirm.options).then(async() => {
          await this.$ajax.post(`/housing/source/backstage/updown`, { id: row.id, shelfStatus: event })
          this.$message.success('操作成功!')
          this.getRequest()
        })
      },
      /**
       * 省市区变化事件
       */
      handleChange(row) {
        const entity = this.$refs.cascader.getCheckedNodes()[0]
        const list = entity && entity.pathLabels && entity.pathLabels.length > 0 ? entity.pathLabels : [null, null, null]
        let arr = ['province', 'city', 'county']
        for (let i = 0; i < list.length; i++) this.queryData[arr[i]] = list[i]
      },
      /**
       * 重置事件
       */
      onReset() {
        this.queryData.pageNum = 1
        this.queryData.province = null
        this.queryData.city = null
        this.queryData.county = null
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },

      /**
       * 列表请求事件
       */
      async getRequest() {
        const params = JSON.parse(JSON.stringify(this.queryData))
        delete params.provinces
        const res = await this.$ajax.get('/housing/source/backstage/list', { params })
        this.queryData.total = res.total
        this.tableList = res.rows
      },

      entityInfoEvent(row) {
        this.$router.push(`/housingManage/housingAdd?detailId=${row.id}&type=2`)
      },
      /**
       * form置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>
  .el-dropdown-link{
    color: #1890ff;
  }
  ::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/ input[type='number'] {
  -moz-appearance: textfield !important;
  }
</style>
